package com.tuananh.radnom.gwt.shared.dto;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.sencha.gxt.data.shared.TreeStore;
import com.sencha.gxt.data.shared.TreeStore.TreeNode;

public class MyBaseDTO implements TreeNode<MyBaseDTO>, Serializable {
	String key;
	String value;
	ArrayList<MyBaseDTO> children;

	
	MyBaseDTO parentDTO = null;

	public MyBaseDTO() {

	}

	public MyBaseDTO(String key, String value) {
		this.key = key;
		this.value = value;
	}

	public ArrayList<MyBaseDTO> getChildren() {
		if (children == null)
			children = new ArrayList<MyBaseDTO>();
		return children;
	}

	public void setChildren(ArrayList<MyBaseDTO> children) {
		this.children = children;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	

	public MyBaseDTO getData() {
		// TODO Auto-generated method stub
		return this;
	}

	public static void validateAndAdd(MyBaseDTO root, String prefix,
			MyBaseDTO child, TreeStore<MyBaseDTO> store) {
		if (root != null && child != null) {
			child.setKey(root.getKey() + "-" + child.getKey());
			store.add(root, child);
		}
	};

	public MyBaseDTO addToTreeStore(MyBaseDTO root, String prefix,
			TreeStore<MyBaseDTO> store) {
		parentDTO = root;
		if (prefix != null && !prefix.equals("")) {
			parentDTO = new MyBaseDTO(root.getKey() + "-" + prefix, prefix);
			validateAndAdd(root, null, parentDTO, store);
		}
		;
		validateAndAdd(parentDTO, null, this, store);
		return this;
	}
}
